﻿using System.Drawing;
using System.Windows;

namespace RealTimeCharts.LineChart
{
    /// <summary>
    /// 标题
    /// </summary>
    public class Title
    {
        public string text;
        public Font font;
        public Brush brush;

        public Rect myRect;
        public Padding padding;

        public Graphics gs;

        public Title() { }
        public Title(Padding padding, string text, Font f, Brush brush)
        {
            this.padding = padding.Clone();
            this.text = text;
            this.font = f;
            this.brush = brush;
        }

        public Title(TitleProperty property)
        {
            this.text = property.title;
            this.font = property.titleTextFont;
            this.brush = property.titleTextBrush;
            this.padding = new Padding(10, 10, 10, 5);

        }

        public Rect GetRemainRect(Graphics g, Rect r)
        {
            Rect paddingRect = GetPaddingRect(r);
            SizeF titleSize = g.MeasureString(text, font);

            paddingRect.Y = paddingRect.Y + titleSize.Height;
            paddingRect.Height = paddingRect.Height - titleSize.Height;
            return paddingRect;
        }

        private Rect GetPaddingRect(Rect r)
        {
            Rect paddingRect = new Rect(r.TopLeft,r.BottomRight);
            paddingRect.X += padding.left;
            paddingRect.Y += padding.top;
            paddingRect.Width -= (padding.left + padding.right);
            paddingRect.Height -= (padding.top + padding.bottom);

            return paddingRect;
        }

        public Rect Show(Graphics g, Rect r)
        {
            Rect paddingRect = GetPaddingRect(r);
            SizeF titleSize = g.MeasureString (text, font);

            PointF startPt = new PointF();
            startPt.X = (float)(paddingRect.Left + paddingRect.Width / 2 - titleSize.Width / 2);
            startPt.Y = (float)(paddingRect.Top);
            g.DrawString(text,font,brush,startPt);

            Rect tmpRect = new Rect(paddingRect.TopLeft,paddingRect.BottomRight);
            tmpRect.Y = tmpRect.Y + titleSize.Height;
            tmpRect.Height = tmpRect.Height - titleSize.Height;
            return tmpRect;
        }
    }
}
